package com.hfzy.ihk.common.core.mybatis;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.toolkit.PackageHelper;
import com.hfzy.ihk.common.core.mybatis.myTypeHandler.CustomEnumTypeEnumHandler;
import com.hfzy.ihk.common.facade.CustomEnumType;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.*;

import javax.annotation.PostConstruct;
import java.util.HashSet;
import java.util.Set;

import static org.springframework.util.StringUtils.tokenizeToStringArray;

/**
 * 这个方法并没有效，改成了配置文件的 default-enum-type-handler 属性引用 czs
 *
 * @author wws
 */
@Configuration
@DependsOn("sqlSessionFactory")
@AutoConfigureAfter(MybatisPlusAutoConfiguration.class)
@Slf4j
public class TypeHandlerRegister {

    @Value("${mybatis-plus.type-enums-package}")
    String  typeEnumsPackage;

    @Autowired
    SqlSessionFactory sqlSessionFactory;

    @PostConstruct
   private void init() {
        TypeHandlerRegistry registry = sqlSessionFactory.getConfiguration().getTypeHandlerRegistry();

        Set<Class> classes;
        if (typeEnumsPackage.contains(StringPool.STAR) && !typeEnumsPackage.contains(StringPool.COMMA)
                && !typeEnumsPackage.contains(StringPool.SEMICOLON)) {
            classes = PackageHelper.scanTypePackage(typeEnumsPackage);
            if (classes.isEmpty()) {
                log.warn("Can't find class in '[" + typeEnumsPackage + "]' package. Please check your configuration.");
            }
        } else {
            String[] typeEnumsPackageArray = tokenizeToStringArray(this.typeEnumsPackage,
                    ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
            Assert.notNull(typeEnumsPackageArray, "not find typeEnumsPackage:" + typeEnumsPackage);
            classes = new HashSet<>();
            for (String typePackage : typeEnumsPackageArray) {
                Set<Class> scanTypePackage = PackageHelper.scanTypePackage(typePackage);
                if (scanTypePackage.isEmpty()) {
                    log.warn("Can't find class in '[" + typePackage + "]' package. Please check your configuration.");
                } else {
                    classes.addAll(PackageHelper.scanTypePackage(typePackage));
                }
            }
        }
        for (Class cls : classes) {
            if (cls.isEnum()) {
              if (CustomEnumType.class.isAssignableFrom(cls)) {
                    // 接口方式
                    registry.register(cls, CustomEnumTypeEnumHandler.class);
                    log.debug("reg CustomEnumType: {}", cls.getName());
               }
            }
        }
    }

}
